package com.uphone.magicleafteacher.util;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 联动变化状态
 */
public class CheckButton extends android.support.v7.widget.AppCompatButton {

    private int countViews = 0;
    private int addCount = 0;

    public CheckButton(Context context) {
        this(context, null);
    }

    public CheckButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CheckButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setSelected(false);
    }

    public void setRelaViews(final TextView... textViews) {
        setRelaViews(null, textViews);
    }

    public void setRelaViews(ChangeCallback callback, final TextView... textViews) {
        countViews = 0;
        for (TextView textView : textViews) {
            textView.addTextChangedListener(new TextWatcher() {
                boolean isEmpty = true;

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    if (TextUtils.isEmpty(s.toString())) {
                        isEmpty = true;
                    } else {
                        isEmpty = false;
                    }
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!TextUtils.isEmpty(s.toString())) {
                        if (isEmpty) {
                            countViews++;
                            if (countViews == textViews.length + addCount) {
                                //变红
                                setSelected(true);
                            }
                        }
                    } else {
                        if (!isEmpty) {
                            countViews--;
                            //变灰
                            setSelected(false);
                        }
                    }
                    if (callback != null)
                        callback.change(textView, s);
                }
            });

        }

        setOnClickListener(v -> {
            if (callback != null) {
                boolean enable = true;
                for (TextView textView : textViews) {
                    if (TextUtils.isEmpty(textView.getText().toString().trim())) {
                        enable = false;
                        break;
                    }
                }
                callback.click(enable);
            }
        });
    }

    public void addCountNum(int count) {
        this.addCount = count;
    }

    public void add() {
        countViews++;
    }

    public void cut() {
        countViews--;
    }


    public static abstract class ChangeCallback {
        public void change(TextView view, Editable s) {
        }

        public abstract void click(boolean enable);
    }

}
